package com.ruoyi.bus.controller;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.bus.domain.TransUnit;
import com.ruoyi.bus.service.ITransUnitService;
import com.ruoyi.bus.service.NoGenerateServlce;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bus.domain.TransAgreement;
import com.ruoyi.bus.service.ITransAgreementService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物流协议Controller
 *
 * @author ruoyi
 * @date 2020-04-09
 */
@Controller
@RequestMapping("/bus/transAgreement")
public class TransAgreementController extends BaseController {
    private String prefix = "bus/transAgreement";

    @Autowired
    private ITransAgreementService transAgreementService;
    @Autowired
    private ITransUnitService transUnitService;

    @Autowired
    private NoGenerateServlce noGenerateServlce;

    @RequiresPermissions("bus:transAgreement:view")
    @GetMapping()
    public String transAgreement() {
        return prefix + "/transAgreement";
    }

            /**
         * 查询物流协议列表
         */
        @RequiresPermissions("bus:transAgreement:list")
        @PostMapping("/list")
        @ResponseBody
        public TableDataInfo list(TransAgreement transAgreement) {
            startPage();
            List<TransAgreement> transAgreements=transAgreementService.list(new QueryWrapper<>(transAgreement));
            for (TransAgreement agreement : transAgreements) {
                TransUnit transUnit=transUnitService.getOne(new QueryWrapper<>(new TransUnit().setTransId(agreement.getTransId())));
                if(transUnit!=null){
                    agreement.setTransName(transUnit.getTransName());
                }
            }

            return getDataTable(transAgreements);
        }
    
    /**
     * 导出物流协议列表
     */
    @RequiresPermissions("bus:transAgreement:export")
    @Log(title = "物流协议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TransAgreement transAgreement) {
        List<TransAgreement> list = transAgreementService.list(new QueryWrapper<>(transAgreement));
        ExcelUtil<TransAgreement> util = new ExcelUtil<>(TransAgreement.class);
        return util.exportExcel(list, "transAgreement");
    }

            /**
         * 新增物流协议
         */
        @GetMapping("/add")
        public String add(ModelMap mmap) {
            TransAgreement transAgreement = new TransAgreement();
            transAgreement.setAgreementId(noGenerateServlce.getSeqNo("AG",5));
            mmap.put("transAgreement", transAgreement);
            return prefix + "/edit";
        }
    
    /**
     * 新增保存物流协议
     */
    @RequiresPermissions("bus:transAgreement:add")
    @Log(title = "物流协议", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TransAgreement transAgreement) {
        return toAjax(transAgreementService.save(transAgreement));
    }

    /**
     * 修改物流协议
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        TransAgreement transAgreement = transAgreementService.getById(id);
        TransUnit transUnit=transUnitService.getOne(new QueryWrapper<>(new TransUnit().setTransId(transAgreement.getTransId())));
        if(transUnit!=null){
            transAgreement.setTransName(transUnit.getTransName());
        }
        mmap.put("transAgreement", transAgreement);
        return prefix + "/edit";
    }

    /**
     * 修改保存物流协议
     */
    @RequiresPermissions("bus:transAgreement:edit")
    @Log(title = "物流协议", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TransAgreement transAgreement) {
        return toAjax(transAgreementService.updateById(transAgreement));
    }

            /**
         * 删除物流协议
         */
        @RequiresPermissions("bus:transAgreement:remove")
        @Log(title = "物流协议", businessType = BusinessType.DELETE)
        @PostMapping( "/remove")
        @ResponseBody
        public AjaxResult remove(String ids) {
            return toAjax(transAgreementService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
        }
        }
